package cn.hiapi.publics.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicController;
import cn.hiapi.basic.jpa.pageable.PageRequest;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.interfaces.user.IUserService;
import cn.hiapi.publics.vo.ShopBaseVo;
import cn.hiapi.shop.entity.shop.Shop;
import cn.hiapi.shop.service.shop.ShopService;
import cn.hiapi.shopkeeper.entity.activity.Activity;
import cn.hiapi.shopkeeper.entity.order.ActivityGroupOrder;
import cn.hiapi.shopkeeper.service.activity.ActivityService;
import cn.hiapi.shopkeeper.service.order.ActivityGroupOrderService;
import cn.hiapi.shopkeeper.service.order.ActivityOrderService;
import cn.hiapi.user.vo.GroupShareVo;
import cn.hiapi.utils.JsonUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2022/12/31 15:37
 */
@RestController("shopkeeperActivityGroupController")
@RequestMapping("/public/activity-group")
public class ActivityGroupController extends BasicController {

    private final DispatchContext dispatchContext;

    public ActivityGroupController(DispatchContext dispatchContext) {
        this.dispatchContext = dispatchContext;
    }

    @ApiOperation(value = "团购信息", notes = "团购信息", nickname = "AdinZ")
    @GetMapping("/group-info")
    public ResponseEntity<GroupShareVo> groupInfo(@RequestParam("id") long gid) {
        ActivityGroupOrder order = this.dispatchContext.getServiceOne(ActivityGroupOrderService.class).get(gid);
        if (order == null) return super.to404();
        Activity activity = this.dispatchContext.getServiceOne(ActivityService.class).get(order.getAid());
        if (activity == null) return toError("活动不存在了");


        GroupShareVo vo = order.copy(new GroupShareVo());
        Object buyer = this.dispatchContext.getServiceOne(IUserService.class).fieldBase(order.getBuyerId());
        vo.setBuyer(buyer);
        List<JSONObject> items = this.dispatchContext.getServiceOne(ActivityOrderService.class).groupUserList(order.getId(), null);
        vo.setItems(items);

        vo.setCostPrice(activity.getPrice());
        vo.setStartTime(activity.getStartTime());
        vo.setEndTime(activity.getEndTime());
        vo.setUseStart(activity.getUseStart());
        vo.setUseEnd(activity.getUseEnd());
        vo.setAid(order.getId());
        vo.setId(activity.getId());

        Shop shop = this.dispatchContext.getServiceOne(ShopService.class).get(order.getShopId());
        if (shop != null) vo.setShop(shop.copy(new ShopBaseVo()));
        return toSuccess(vo);
    }


    @ApiOperation(value = "正在进行中的拼团", notes = "正在进行中的拼团", nickname = "AdinZ")
    @GetMapping("/group-list")
    public ResponseEntity<List> groupList(@RequestParam("aid") long aid, @RequestParam(value = "page", required = false, defaultValue = "1") int page, @RequestParam(value = "size", required = false, defaultValue = "20") int size) {

        ResponseEntity<List<ActivityGroupOrder>> response = this.dispatchContext.getServiceOne(ActivityGroupOrderService.class).findPage(new QueryWrapper().eq("aid", aid)
                .eq("status", 1), PageRequest.of(page, size, Sort.Direction.ASC, "expire"));

        List<JSONObject> collect = response.getData().stream().map(e -> JsonUtils.builder()
                .put("buyer", this.dispatchContext.getServiceOne(IUserService.class).fieldBase(e.getBuyerId()))
                .put("groupNum", e.getGroupNum())
                .put("buyNum", e.getBuyNum())
                .put("expire", e.getExpire())
                .put("gid", e.getId())
                .build()).collect(Collectors.toList());
        return toSuccess(collect, response.getTotal(), response.getTotalPage());
    }


}
